package response

import (
	"PolarDay/common/returnCode/bCode"
	"PolarDay/common/returnCode/eCode"
	"PolarDay/common/returnCode/hCode"
	"PolarDay/common/returnCode/pCode"
	"PolarDay/common/returnCode/tCode"

	"github.com/gin-gonic/gin"
)

// ResponseData 数据返回结构体
type ResponseData struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

/**
 * 406
 */
func RespB406(c *gin.Context, businessCode bCode.BusinessCode, errorCode eCode.ErrorCode, data interface{}) {
	RespBusiData(c, hCode.NotAcceptable, tCode.Business, businessCode, errorCode, "", data)
}

func RespB406s(c *gin.Context, businessCode bCode.BusinessCode, errorCode eCode.ErrorCode, errorMsg string, data interface{}) {
	RespBusiData(c, hCode.NotAcceptable, tCode.Business, businessCode, errorCode, errorMsg, data)
}

func RespP406(c *gin.Context, programCode pCode.ProgramCode, errorCode eCode.ErrorCode, data interface{}) {
	RespProgramData(c, hCode.NotAcceptable, tCode.Business, programCode, errorCode, "", data)
}

func RespP406s(c *gin.Context, programCode pCode.ProgramCode, errorCode eCode.ErrorCode, errorMsg string, data interface{}) {
	RespProgramData(c, hCode.NotAcceptable, tCode.Business, programCode, errorCode, errorMsg, data)
}

/**
 * 500
 */
func RespP500(c *gin.Context, programCode pCode.ProgramCode, errorCode eCode.ErrorCode, data interface{}) {
	RespProgramData(c, hCode.InternalError, tCode.Business, programCode, errorCode, "", data)
}

/**
 * 200
 */
func RespB200(c *gin.Context, businessCode bCode.BusinessCode, data interface{}) {
	RespBusiData(c, hCode.Ok, tCode.Business, businessCode, eCode.P0000, "", data)
}
func RespB200m(c *gin.Context, businessCode bCode.BusinessCode, msg string, data interface{}) {
	RespBusiData(c, hCode.Ok, tCode.Business, businessCode, eCode.P0000, msg, data)
}

func Resp200(c *gin.Context, businessCode bCode.BusinessCode) {
	RespBusiData(c, hCode.Ok, tCode.Business, businessCode, eCode.P0000, "", nil)
}

// 业务逻辑错误
func RespBusiData(c *gin.Context, httpCode hCode.HttpCode, typeCode tCode.TypeCode, businessCode bCode.BusinessCode, errorCode eCode.ErrorCode, errorMsg string, data interface{}) {
	businessMsg := bCode.GetBusinessMsg(businessCode)
	if errorMsg == "" {
		errorMsg = eCode.GetErrMsg(errorCode)
	}
	resp := ResponseData{
		Code:    int(httpCode) + int(typeCode) + int(businessCode) + int(errorCode),
		Message: businessMsg + errorMsg,
		Data:    data,
	}
	//RespJSON(c, int(httpCode)/1000000, resp)
	RespJSON(c, 200, resp)
}

// 程序错误
func RespProgramData(c *gin.Context, httpCode hCode.HttpCode, typeCode tCode.TypeCode, programCode pCode.ProgramCode, errorCode eCode.ErrorCode, errorMsg string, data interface{}) {
	businessMsg := pCode.GetProgramMsg(programCode)
	if errorMsg == "" {
		errorMsg = eCode.GetErrMsg(errorCode)
	}
	resp := ResponseData{
		Code:    int(httpCode) + int(typeCode) + int(programCode) + int(errorCode),
		Message: businessMsg + errorMsg,
		Data:    data,
	}
	//RespJSON(c, int(httpCode)/1000000, resp)
	RespJSON(c, 200, resp)
}

// RespJSON 返回JSON数据
func RespJSON(c *gin.Context, httpCode int, resp interface{}) {
	c.JSON(httpCode, resp)
	c.Abort()
}
